import { IHttpResponse } from '@ibiz-template/core';
import {
  ControlVO,
  EventBase,
  IEditFormController,
} from '@ibiz-template/runtime';
import { isArray } from 'lodash-es';
import { EditFormService } from '../../../edit-form';
import { FormMDCtrlControlController } from '../form-mdctrl-control/form-mdctrl-control.controller';

/**
 * 表单多数据部件表单类型控制器
 *
 * @author zk
 * @date 2023-07-28 08:07:57
 * @export
 * @class FormMDCtrlFormController
 * @extends {EditFormController}
 * @implements {IFormController}
 */
export class FormMDCtrlFormController extends FormMDCtrlControlController {
  /**
   * 表格控制器
   *
   * @author zk
   * @date 2023-07-30 07:07:18
   * @type {IGridController}
   * @memberof FormMDCtrlGridController
   */
  controlControllerArray: IEditFormController[] | null[] = [];

  /**
   * uiState
   *
   * @author zk
   * @date 2023-08-01 03:08:16
   * @type {{ data: IData[] }}
   * @memberof FormMDCtrlFormController
   */
  state: { data: IData[] } = { data: [] };

  /**
   * 编辑表单服务
   * @author lxm
   * @date 2023-05-15 11:03:34
   * @type {EditFormService}
   */
  service!: EditFormService;

  /**
   * 表格挂载后把控制器抛出来
   *
   * @author zk
   * @date 2023-07-30 07:07:25
   * @param {EventBase} event
   * @memberof FormMDCtrlGridController
   */
  onControlMounted(event: EventBase, index: number): void {
    const formController = event.ctrl as IEditFormController;
    this.controlControllerArray[index] = formController;
    formController.load();
  }

  /**
   * 初始化
   *
   * @author zk
   * @date 2023-07-25 10:07:11
   * @protected
   * @return {*}  {Promise<void>}
   * @memberof FormMDCtrlController
   */
  async onInit(): Promise<void> {
    super.onInit();
    // 实例部件服务
    this.service = new EditFormService(this.model.contentControl!);
    await this.service.init(this.parent.form.context);
  }

  /**
   * 执行查询多条数据的方法
   *
   * @author lxm
   * @date 2022-08-31 17:08:41
   * @param {IParams} context 上下文
   * @param {IParams} [params={}] 视图参数
   * @returns {*}  {Promise<IHttpResponse>}
   */
  async fetch(
    context: IParams,
    params: IParams = {},
  ): Promise<IHttpResponse<ControlVO[]>> {
    const fetchAction = 'fetchdefault';
    let res = await this.service.exec(fetchAction, context, params);
    res = this.handleResponse(res);
    return res as IHttpResponse<ControlVO[]>;
  }

  /**
   * 处理响应(数组类型)
   *
   * @author lxm
   * @date 2022-08-31 17:08:13
   * @param {IHttpResponse} res
   * @returns {*}  {IHttpResponse}
   */
  handleResponse(response: IHttpResponse): IHttpResponse {
    const res = response as IHttpResponse<IData[] | IData>;
    if (res.ok) {
      if (isArray(res.data)) {
        res.data = (res.data as IData[]).map(item =>
          this.service.toUIData(item),
        );
      } else {
        res.data = this.service.toUIData(res.data);
      }
    }
    return res;
  }

  /**
   * 初始化数据
   *
   * @author zk
   * @date 2023-07-30 08:07:32
   * @memberof FormMDCtrlContentTypeController
   */
  async initData(): Promise<void> {
    const res = await this.fetch(
      this.parent.form.context,
      this.parent.form.params,
    );
    this.state.data = res.data;
  }

  /**
   * 数据删除
   *
   * @author zk
   * @date 2023-07-30 05:07:32
   * @param {number} index
   * @memberof FormMDCtrlController
   */
  onDataRemove(index: number): void {
    const c = this.controlControllerArray[index];
    if (!c) {
      return;
    }
    c.remove();
  }

  /**
   * 处理表单数据变化
   *
   * @author zk
   * @date 2023-07-30 05:07:03
   * @param {IData[]} data
   * @param {number} index
   * @memberof FormMDCtrlFormController
   */
  onFormChange(data: IData[], index: number): void {
    const c = this.controlControllerArray[index];
    if (!c) {
      return;
    }
    c.save();
    if (this.state.data[index]) {
      this.state.data[index] = data[0];
    }
    this.parent.state.contentCtrlData = this.state.data;
    this.parent.setFormDataValue();
  }

  /**
   * 表单删除成功
   *
   * @author zk
   * @date 2023-08-01 03:08:47
   * @param {number} index
   * @memberof FormMDCtrlFormController
   */
  onFormRemoveSuccess(index: number): void {
    this.state.data.splice(index, 1);
    this.parent.state.contentCtrlData = this.state.data;
    this.parent.setFormDataValue();
  }

  /**
   * 数据插入
   *
   * @author zk
   * @date 2023-07-31 12:07:27
   * @param {IData} [data]
   * @memberof FormMDCtrlContentTypeController
   */
  onDataInsert(): void {
    this.state.data.push({});
    this.parent.state.contentCtrlData = this.state.data;
    this.parent.setFormDataValue();
  }
}
